/*
 * Copyright 1999-2010 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.alibaba.aone.casestudy.module.screen;

import static com.alibaba.citrus.turbine.util.TurbineUtil.getTurbineRunData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.WebConstant;
import com.alibaba.aone.casestudy.common.entity.AoneUser;
import com.alibaba.aone.casestudy.common.entity.CaseStudyConditions;
import com.alibaba.aone.casestudy.common.entity.Casestudy;
import com.alibaba.aone.casestudy.service.AoneUserService;
import com.alibaba.aone.casestudy.service.CasestudyService;
import com.alibaba.aone.casestudy.service.FailureService;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.TurbineRunDataInternal;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.common.lang.StringUtil;

/**
 * case study screen类
 * 
 * @author gaojian 2011-4-25 上午10:15:39
 */
public class CaseStudyList {

	@Autowired
	private CasestudyService casestudyService;
	@Autowired
	private FailureService failureService;
	@Autowired
	private AoneUserService userService;
	@Autowired
	private HttpServletRequest request;

	public void execute(@Param("initiator") String initiator,
			@Param("initDepartment") String initDepartment,
			@Param("status") Integer[] statusArray,
			@Param("category") Integer[] categoryArray,
			@Param("analysisTimeFrom") String analysisTimeFromTemp,
			@Param("analysisTimeTo") String analysisTimeToTemp,
			@Param(name = "page", defaultValue = "1") Integer page,
			Context context) throws ParseException {
		initDepartment = StringUtil.isEmpty(initDepartment) ? getCurrentDepart("49706")
				: initDepartment;
		if (paramsLegal(initiator, initDepartment, statusArray, categoryArray,
				analysisTimeFromTemp, analysisTimeToTemp, page)) {

			CaseStudyConditions caseStudyConditions = new CaseStudyConditions();
			List<Integer> statuses = new ArrayList<Integer>();
			statuses.addAll(Arrays.asList(statusArray));
			List<Integer> categories = new ArrayList<Integer>();
			categories.addAll(Arrays.asList(categoryArray));

			Date analysisTimeFrom = null;
			Date analysisTimeTo = null;
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

			analysisTimeFrom = StringUtils.isEmpty(analysisTimeFromTemp) ? null
					: format.parse(analysisTimeFromTemp);
			analysisTimeTo = StringUtils.isEmpty(analysisTimeToTemp) ? null
					: format.parse(analysisTimeToTemp);

	
			caseStudyConditions.setInitiator(initiator);
			caseStudyConditions.setInitDepartment(initDepartment);
			caseStudyConditions.setCategories(categories);
			caseStudyConditions.setStatuses(statuses);
			caseStudyConditions.setAnalysisTimeFrom(analysisTimeFrom);
			caseStudyConditions.setAnalysisTimeTo(analysisTimeTo);
			caseStudyConditions.setCurrentPage(page);
			List<Casestudy> caseStudies = casestudyService
					.search(caseStudyConditions);
			
			context.put("casestudies", caseStudies);
			context.put("failureService", failureService);
			context.put("query", caseStudyConditions);

		}
	}

	public boolean paramsLegal(Object... params) {
		for (Object param : params) {
			if (param != null) {
				return true;
			}
		}
		return false;
	}

	public String getCurrentDepart(String empno) {
		TurbineRunDataInternal rundata = (TurbineRunDataInternal) getTurbineRunData(request);
		String cookie = rundata.getCookies().getString(
				WebConstant.AONE_COOKIE_LOGIN_NAME);System.out.println(cookie);
		AoneUser user = userService.getAoneUserByAccount(cookie);
		return user.getDepartment();
	}
}